package com.funny.stock.backtest.service;

import com.funny.stock.backtest.dao.pojo.BackTestPlanDO;
import com.funny.stock.backtest.dao.repo.BackTestPlanRepo;
import com.funny.stock.backtest.vo.BackTestPlanVO;
import com.funny.stock.base.service.BaseCrudServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

/**
 * @Description  
 * @Author  pengl
 * @Date 2020-05-17 
 */

@Service
public class BackTestPlanService extends BaseCrudServiceV2<BackTestPlanVO,BackTestPlanDO,Long> {

	@Autowired
	BackTestPlanRepo repository;

	@Autowired
//	@Qualifier("asyncTaskExecutor")
	TaskExecutor taskExecutor;

	@Autowired
	BackTestPlanTaskHelper taskHelper;

	protected JpaRepository<BackTestPlanDO, Long> setupJpaRepository() {
		return repository;
	}

	@Override
	protected Class<BackTestPlanDO> getDOClass() {
		return BackTestPlanDO.class;
	}

	@Override
	protected Class<BackTestPlanVO> getVOClass() {
		return BackTestPlanVO.class;
	}

	public BackTestPlanVO startBackTest(BackTestPlanVO backTestPlanVO){
		BackTestPlanDO backTestPlanDO = repository.save(convertDO(backTestPlanVO));

		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				taskHelper.startBackTest(backTestPlanDO);
			}
		});

		return convertVO(backTestPlanDO);
	}


}
